package com.example.demo.controller;

import com.example.demo.common.domain.Employee;
import com.example.demo.common.domain.Leave_Record;
import com.example.demo.common.domain.OutRecord;
import com.example.demo.common.util.Result;
import com.example.demo.common.util.StringUtil;
import com.example.demo.service.DateService;
import com.example.demo.service.LeaveService;
import com.example.demo.service.OutService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class OutController {
    @Autowired
    @Qualifier("ds")
    DateService dateService;

    @Autowired
    @Qualifier("outServiceImpl")
    OutService outService;



    @RequestMapping("/out")
    public String leave(HttpSession session, Model model){
        if(check(session))
            return "out";
        else
            return "login";
    }

    @RequestMapping("/out2")//搜索所有的外出记录，并将一定范围内的外出记录渲染到前端
    public String leave2(HttpSession session,Model model,
                         @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                         @RequestParam(required = false,defaultValue = "6") Integer pageSize
    ){
        PageHelper.startPage(pageNo,pageSize);
        if(!check(session)){
            return "login";
        }
        List<OutRecord> outRecords = outService.selectAll();

        PageInfo<OutRecord> pageInfo = new PageInfo(outRecords);
        pageInfo.setPageSize(pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "out2";
    }
    @RequestMapping("/out3")//通过传入的外出号，将该外出信息渲染到前端
    public String leave3(HttpSession session,Model model,Integer oid){
        if(!check(session)){
            return  "login";
        }

        OutRecord outrecord = outService.selectByPrimaryId(oid);
        model.addAttribute("out",outrecord);

        String percent = "0";
        if(outrecord.getOgeneralagree() == 1){
            percent = "100";
        }else if(outrecord.getOdeputyagree() == 1){
            percent = "66";
        }else{
            percent = "33";
        }

        model.addAttribute("percent",percent);
        return "out3";
    }


    public boolean check(HttpSession session){
        if(session.getAttribute("employee")!=null)
            return true;
        else
            return false;
    }
    @RequestMapping("/subOut")
    @ResponseBody//通过前端传入的用户信息，产生一条新的外出记录，并将外出记录存入数据库中
    public Map subLeave(HttpSession session, String startDate, String startTime,
                        String endDate, String endTime, String reason){
        Employee e = (Employee)session.getAttribute("employee");


        startDate = StringUtil.rmQuote(startDate);
        startTime = StringUtil.rmQuote(startTime);
        endDate = StringUtil.rmQuote(endDate);
        endTime = StringUtil.rmQuote(endTime);
        reason = StringUtil.rmQuote(reason);


        Date from = dateService.parseDate(startDate, startTime);
        Date to = dateService.parseDate(endDate,endTime);
//        String format = dateService.dateFormat(date);
        OutRecord outRecord = new OutRecord();

        outRecord.setOdescription(reason);
        outRecord.setOstarttime(from);
        outRecord.setOendtime(to);

        outRecord.setEid(e.getEid());
        outRecord.setOdescription(reason);
        outService.insertSelective(outRecord);


        return Result.success(true);
    }
}
